//
//  TimestampHelper.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/7.
//

import Foundation


class TimestampHelper {
    // 获取当前时间戳（秒级）
    static func currentTimestamp() -> Int {
        return Int(Date().timeIntervalSince1970)
    }
    
    // 获取当前毫秒级时间戳
    static func currentMilliTimestamp() -> String {
        return  String(Int(Date().timeIntervalSince1970)*1000)
    }
    
    
    // 时间戳转日期字符串
    static func timestampToStringDay() -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        return formatter.string(from: Date())
    }
    
    
    // 时间戳转日期字符串
    static func timestampToString(_ timestamp: Int, format: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
        return dateToString(date, format: format)
    }
    
    // 日期转字符串
    static func dateToString(_ date: Date, format: String, timeZone: TimeZone? = nil) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.timeZone = timeZone ?? TimeZone.current
        return formatter.string(from: date)
    }
    
    // 字符串转时间戳
    static func stringToTimestamp(_ string: String, format: String) -> Int? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        guard let date = formatter.date(from: string) else { return nil }
        return Int(date.timeIntervalSince1970)
    }
    
    static func getMonthName() -> String {
        return String(Calendar.current.component(.month, from: Date()))
    }
    
    
    static func getDayName() -> Int {
        return Calendar.current.component(.day, from: Date())
    }
    
    
    static  func convert(_ string: String)-> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        if let date = formatter.date(from: string) {
            print("转换成功: \(date)")
            return date
        } else {
            print("格式不匹配")
            return nil
        }
    }
    
    // 字符串转时间
    static  func convertHourM(_ timeString: String)-> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"  // 24小时制格式
//        formatter.timeZone = TimeZone(secondsFromGMT: 0)  // 使用UTC时区
        if let date = formatter.date(from: timeString) {
            print("转换结果: \(date)")  // 输出: 2000-01-01 10:12:00 +0000
            return date
        } else {
            print("时间格式错误")
            return nil
        }
    }
    
    //字符串转时间戳
    static func stringToTimestamp(dateString: String, format: String = "yyyy/MM/dd HH:mm:ss") -> Int? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
//        formatter.locale = Locale(identifier: "zh_CN")
        guard let date = formatter.date(from: dateString) else {
            return nil
        }
        return  Int(date.timeIntervalSince1970)
    }
    
}
